HttpProxy : Http proxy
HttpProxy is an HTTP programmable proxying library that supports websocket and socket.io. It is suitable for implementing components such as reverse proxies and load balancers.
User can use the following code to import the HttpProxy module.
var HttpProxy = require('http_proxy');
Support
The following shows HttpProxy module APIs available for each permissions.
| User Mode | Privilege Mode | |
|---|---|---|
| HttpProxy.create | ● | |
| proxy.web | ● | |
| proxy.ws | ● | |
| proxy.stop | ● | 
HttpProxy Class
HttpProxy.create([opts])
- opts{Object} The following are the options:- httpTarget{String} Http target URL. For HTTP[S] proxy.
- httpTlsOpt{Object} TLS securely connections options.** default: undefined**, means use TCP connection. For HTTP[S] proxy.
- wsTarget{String} Websocket target URL. For WS[S] proxy.
- wsTlsOpt{Object} TLS securely connections options.** default: undefined**, means use TCP connection. For WS[S] proxy.
- tlsOpt{Object} TLS securely connections options. If- httpTargetor- wsTlsOptis not defined,- tlsOptwill replace them.** default: undefined**,
- xfwd{Boolean} If adds "X-Forward" headers. default: false.
- headers{Object} Object with extra headers to be added to target requests.
- prependPath{Boolean} Specify whether you want to prepend the target's path to the proxy path.default: true.
- ignorePath{Boolean} Specify whether you want to ignore the proxy path of the incoming request (You will have to append / manually if required). default: false.
- changeOrigin{Boolean} Changes the origin of the host header to the target URL. default: false.
 
Example
var HttpProxy = require('http_proxy');
var proxy = HttpProxy.create({httpTarget: 'http://192.168.7.32:3010'});
HttpProxy Object
proxy.web(req, res[, opts])
- req{HttpInput} HttpInput object.
- res{HttpOutput} HttpOutput object.
- opts{Object} Reference- HttpProxy.create optsargument. This options will override the options of- HttpProxy.create.
Used for proxying regular HTTP(S) requests.
Example
var socket = require('socket');
var HttpServer = require('http_server');
var HttpProxy = require('http_proxy');
var proxy = HttpProxy.create({httpTarget: 'http://192.168.7.32:3010'});
function handle(req, res) {
  proxy.web(req, res);
}
var server = HttpServer.createServer('http', handle, 0,               
             socket.sockaddr(socket.INADDR_ANY, 80));
proxy.ws(req, net, cb[, opts])
- req{HttpInput} HttpInput object.
- net{Socket} Base socket object.
- cb{Function} Callback function.
- opts{Object} Reference- HttpProxy.create optsargument. This options will override the options of- HttpProxy.create.
Used for proxying WS(S) requests.
Example
var HttpServer = require('http_server');
var socket = require('socket');
var HttpProxy = require('http_proxy');
var proxy = HttpProxy.create({wsTarget: 'ws://192.168.7.32:3010'});
server.on('upgrade', function (req, net, cb) {
  proxy.ws(req, net, cb);
});
var server = HttpServer.createServer('ws', handle, 0, 
             socket.sockaddr(socket.INADDR_ANY, 80));
proxy.stop()
A function that closes and stops proxy.
HttpProxy Events
request
This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. It's argument:
- proxyReq{HttpClient} Proxy request object. Reference to httpclient for more informance.
- req{HttpServerRequest} The origin request object. Reference to [httpserver] httpserver for more informance.
- res{HttpServerResponse} The origin response object. Reference to httpserver for more informance.
Example
var proxy = HttpProxy.create({httpTarget: 'http://192.168.7.32:3010'});
proxy.on('request', function(proxyReq, req, res) {
  proxyReq.setHeader('X-Special-Proxy-Header', 'foo');
});
response
This event is emitted if the request to the target got a response. It's argument:
- proxyReq{HttpClient} Proxy request object. Reference to httpclient for more informance.
- req{HttpServerRequest} The origin request object. Reference to httpserver for more informance.
- res{HttpServerResponse} The origin response object. Reference to httpserver for more informance.
Example
var proxy = HttpProxy.create({httpTarget: 'http://192.168.7.32:3010'});
proxy.on('response', function (proxyRes, req, res) {
  res.setHeader('X-Special-Proxy-Header', 'bar');
});
Example
var HttpServer = require('http_server');
var socket = require('socket');
var HttpProxy = require('http_proxy');
var iosched = require('iosched');
var proxy = new HttpProxy({httpTarget: 'http://192.168.7.32:3010',
                           wsTarget: 'ws://192.168.7.32:3010'});
function handle(req, res) {
  proxy.web(req, res);
}
var server = HttpServer.createServer('proxy', handle, 0, 
             socket.sockaddr(socket.INADDR_ANY, 80));
server.on('upgrade', function (req, net, cb) {
  proxy.ws(req, net, cb);
});
server.start();
while(true) {
  iosched.poll();
}




 陕公网安备61019002002605号
陕公网安备61019002002605号